class Solution
{
public:
    struct UnionFind
    {
        vector<int> parent;
        UnionFind(int n)
        {
            parent.resize(n, -1);
        }
        int find(int n)
        {
            vector<int> son;
            while (parent[n] >= 0)
            {
                son.push_back(n);
                n = parent[n];
            }
            for (auto e : son) parent[e] = n;
            return n;
        }

        void merger(int x, int y)
        {
            int rootx = find(x);
            int rooty = find(y);
            if (rootx == rooty) return;
            else parent[rootx] = rooty;
        }
    };
    bool equationsPossible(vector<string>& equations)
    {
        UnionFind uf(26);
        for (auto& equation : equations)
        {
            if (equation[1] == '=')
            {
                uf.merger(equation[0] - 'a', equation[3] - 'a');
            }
        }
        for (auto& equation : equations)
        {
            if (equation[1] != '=')
            {
                int rootx = uf.find(equation[0] - 'a');
                int rooty = uf.find(equation[3] - 'a');
                if (rootx == rooty)return false;
            }
        }
        return true;
    }
};